#!/usr/bin/env python3

import codecs
import json
import pymongo
import time
import http.client, urllib.request, urllib.parse, urllib.error, base64,urllib.response
from config import *
from multiprocessing import Pool

#配置mongodb的信息
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]

'''
调用接口获取数据
'''
def get_data(university_name):
	headers = {
		# e321e00f876b41989a89b56887161602
		'Ocp-Apim-Subscription-Key': '3d50680d6f7f4e7b9bd07495d029fefd',
	}
	params = urllib.parse.urlencode({
		'expr': "And(composite(AA.AfN=='" + university_name + "'),Y>2008)",
		'model': 'latest',
		'count': '500',
		'offset': '43000',
		'orderby': 'Y',
		'attributes': 'Ti,Y,CC,ECC,AA.AuN,AA.AuId,AA.AfN,AA.AfId,AA.S,F.FN,F.FId,C.CN,C.CId',
	})

	conn = http.client.HTTPSConnection('api.labs.cognitive.microsoft.com')
	conn.request("GET", "/academic/v1.0/evaluate?%s"%params,"",headers)
	response = conn.getresponse()

	if response.status == 200:
		# print(response.status)
		json_data = str(response.read(),encoding = "utf-8")
		conn.close()
		return json_data
	else:
		print("调用接口失败!")
		print(university_name)
		print(response.read())
		conn.close()
		return None

		# cellular differentiation

'''
写入json文件
'''
def write_in_json(json_data,university_name,conference_name):
	f = codecs.open(university_name + conference_name + ".json", 'w', 'utf8')
	f.write(json_data)
	f.close()

'''
将字符串形式转为json格式,并逐条保存数据
'''
def str_to_json(json_data,university_name):
	json_entities_data = json_data["entities"]
	for item in json_entities_data:
		# print(item["C"])
		save_to_mongo(item,university_name)


'''
插入到mongodb中
'''
def save_to_mongo(result,university_name):
	# print(result)
	# db_name = university_name + conference_name
	if db[university_name].save(result):
		# print("存储到MongoDB成功")
		return True
	return False


'''
主函数
'''
def main(university_name):
	json_data = get_data(university_name)
	# print(json_data)
	f = codecs.open('res.json', 'w', 'utf8')
	f.write(json_data)
	f.close()
	json_data = json.loads(json_data)
	CN_length = 0
	JN_length = 0
	i = 0
	for item in json_data["entities"]:
		print(item)
	# 	if item.get("Id"):
	# 		print("第" + i + "位是会议论文,名称为:" + item.get("CN"))
	# 		CN_length += 1
	# 	elif item.get("Id"):
	# 		print("第" + i + "位是期刊论文,名称为:" + item.get("JN"))
	# 		JN_length += 1
	# 	i += 1
	# print(CN_length)
	# print(JN_length)
	print(len(json_data["entities"]))
	# str_to_json(json_data,university_name)
	# for conference_name in CONFERENCE_LIST:
	# 	json_data = get_data(university_name,conference_name)
	# 	print(json_data)
		# time.sleep(5)
		# # write_in_json(json_data,university_name,conference_name)
		# json_data = json.loads(json_data)
		# if len(json_data["entities"]) == 0:
		# 	print("学校没那么多的paper!")
		# 	print(university_name)
		# # print(university_name + "的长度为",len(json_data["entities"]))
		# else:
		# 	str_to_json(json_data,university_name,conference_name)


if __name__ == '__main__':
	# pool = Pool()
	# pool.map(main,UNIVERSITY_LIST)
	university_name = 'peking university'
	main(university_name)
